#include "response.h"
#include <string.h>

char * get_ext(char * file_path){
    int len = strlen(file_path);
    char * p = file_path + len;
    do{
        --p;
        if(*p == '.'){
            return ++p;
        }
    }while(p != file_path);

    return NULL;
}

#define CONTENT_TYPE_COUNT 5
char* response_content_type(char * file_path,char*content_type){
    char * ext = get_ext(file_path);
    char *map[][CONTENT_TYPE_COUNT] = {
            {"png","html","js","css","php"},
            {"image/png","text/html","application/x-javascript","text/css","text/html"},
    };
    int count = CONTENT_TYPE_COUNT,i,ifFind=0;
    for(i=0;i<count;i++){
        if(strcmp(ext,map[0][i]) == 0){
            strcpy(content_type,map[1][i]);
            ifFind = 1;
            break;
        }
    }

    if(ifFind == 0){
        strcpy(content_type,"application/octet-stream");
    }
    return content_type;
}